#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5005;
const int MX = 5001;

int n, r;
int a[N][N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> r;
  rep(i, 1, n) {
    int x, y, w;
    cin >> x >> y >> w;
    x++, y++;
    a[x][y] += w;
  }
  rep(i, 1, MX) rep(j, 1, MX) {
    a[i][j] = a[i][j - 1] + a[i - 1][j] - a[i - 1][j - 1] + a[i][j];
  }
  if (r == 0) {
    cout << 0;
    return 0;
  }
  if (r >= MX) {
    cout << a[MX][MX];
    return 0;
  }
  int ans = 0;
  rep(i, r, MX) rep(j, r, MX) {
    int now = a[i][j] - a[i - r][j] - a[i][j - r] + a[i - r][j - r];
    ans = max(ans, now);
  }
  cout << ans;
  return 0;
}